package com.ncloud.act.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ncloud.act.common.ActDataHelper;
import com.ncloud.act.mapper.ChnlAdjustBillMapper;
import com.ncloud.act.service.ChnlAdjustBillService;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.PageHelperUtils;

@Service(retries=-1,timeout=6000)
public class ChnlAdjustBillServiceImpl implements ChnlAdjustBillService{
	@Autowired
	private ChnlAdjustBillTransServiceImpl chnlAdjustBillTransService;
	@Resource
	private ChnlAdjustBillMapper chnlAdjustBillMapper;
	@Override
	public JSONObject insertChnlAdjustBill(JSONObject json) throws ParseException {
		return chnlAdjustBillTransService.insertChnlAdjustBill(json);
	}
	//=================================================================
	@Override
	public JSONObject getChnlAdjustBillInfo(JSONObject json) {
		Integer user_id = 0;
		//Long chnl_id = 0l;
		String start_time="";
		String end_time="";
		if(json.containsKey("userID"))
		{
			user_id=json.getInteger("userID");
		}else{
			throw new UserException("4000", "没有用户编码");
		}
		if(json.containsKey("start_date"))
		{
			start_time=json.getString("start_date");
		}else{
			throw new UserException("4000", "没有开始日期");
		}
		start_time=start_time+" 00:00:00";
		if(json.containsKey("end_date"))
		{
			end_time=json.getString("end_date");
		}else{
			throw new UserException("4000", "没有结束日期");
		}
		end_time=end_time+" 23:59:59";
		JSONArray ja=new JSONArray();
		PageInfo pageInfo =null;
		JSONObject jo = new JSONObject();
		List<JSONObject> chnlAdjustBills = new ArrayList<JSONObject>();
		if(PageHelperUtils.isPage(json)){
			PageHelperUtils.setPageParam(json);
			chnlAdjustBills = chnlAdjustBillMapper.getChnlAdjustBillInfo(user_id, start_time, end_time);
			pageInfo = new PageInfo(chnlAdjustBills);
			jo=JSON.parseObject(JSON.toJSONString(pageInfo));
			ja=jo.getJSONArray("list");
			ActDataHelper.div100(ja, new String[] {"adjust_fee","adjust_balance"});
		}else{
			chnlAdjustBills = chnlAdjustBillMapper.getChnlAdjustBillInfo(user_id, start_time, end_time);
			ja=JSON.parseArray(JSON.toJSONString(chnlAdjustBills));
			ActDataHelper.div100(ja, new String[] {"adjust_fee","adjust_balance"});
			jo.put("results", ja);
		}
		return jo;
	}
}
